package com.qy.controller.app;

import com.qy.base.utils.DateUtil;
import com.qy.model.Member;
import com.qy.model.RewardOrder;
import com.qy.service.MemberService;
import com.qy.service.RewardOrderService;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

@Component
@EnableScheduling
public class TaskTest {
    @Resource
    private RewardOrderService rewardOrderService;

    @Resource
    private MemberService memberService;

    public TaskTest() {
    }

    //取消悬赏
    @Scheduled(cron = "0/5 * * * * ?")
    public void cancelReward() {
        List<RewardOrder> list = rewardOrderService.selectRewardOrderListByState();
        if (list.size() != 0) {
            for (int i = 0; i < list.size(); i++) {
                RewardOrder rewardOrder = list.get(i);
                if ((Long.valueOf(rewardOrder.getSubmit_time()) + 900000L) < (Long.valueOf(DateUtil.getNowTimestamp()))) {
                    rewardOrder.setOrder_state(3);
                    rewardOrder.setCancel_time(DateUtil.getNowTimestamp());
                    rewardOrderService.update(rewardOrder);
                }
            }
        }

    }

    //重置登陆次数
    @Scheduled(cron = "0 0 0 * * ?")
    public void resetLoginNumber() {

        List<Member> memberList = memberService.findAll();
        for (int i = 0; i < memberList.size(); i++) {
            Member member = memberList.get(i);
            member.setLogin_number(0);
            memberService.update(member);
        }
    }

}
